package client

import (
	"encoding/json"
	"tio-proxy-agent/core"
	"tio-proxy-agent/log"
)

type GioClientListener struct{}

func (GioClientListener) OnAfterConnected(ctx *core.ChannelContext) {
	var csp = map[string]string{
		"token":    TunnelInfo.Token,
		"host":     TunnelInfo.Domain,
		"bsId":     ctx.Id,
		"protocol": TunnelInfo.Protocol,
	}
	cspBytes, _ := json.Marshal(csp)

	packet := &BsPacket{
		Type: 5,
		Body: cspBytes,
	}
	ctx.SendData <- packet

}

func (GioClientListener) OnBeforeClose(ctx *core.ChannelContext) {
}

type GioClientForwardListener struct {
	BsId string
}

func (GioClientForwardListener) OnAfterConnected(ctx *core.ChannelContext) {
	log.SugarLogger.Info("和目标服务器连接成功~~")
}

func (listener *GioClientForwardListener) OnBeforeClose(ctx *core.ChannelContext) {
	body := &TcpTunnelBody{
		BsId:     listener.BsId,
		Protocol: "tcp",
	}
	bodyBytes, _ := json.Marshal(body)
	bsPacket := &BsPacket{
		Type: 3,
		Body: bodyBytes,
	}
	ChannelManage["proxy"].SendData <- bsPacket
}

type GioClientTcpForwardListener struct {
	BsId string
}

func (GioClientTcpForwardListener) OnAfterConnected(ctx *core.ChannelContext) {
	log.SugarLogger.Info("和目标服务器连接成功~~")
}

func (listener *GioClientTcpForwardListener) OnBeforeClose(ctx *core.ChannelContext) {
	body := &TcpTunnelBody{
		BsId:     listener.BsId,
		Protocol: "tcp",
	}
	bodyBytes, _ := json.Marshal(body)
	bsPacket := &BsPacket{
		Type: 3,
		Body: bodyBytes,
	}
	ChannelManage["proxy"].SendData <- bsPacket
}
